using BooStudio;
using System;
using System.Collections;
using System.Globalization;
using UnityEngine;
using UnityEngine.UI;

public class DailyReward : MonoBehaviour
{
	public Text timeLabel;

	public Button timerButton;

	public Image _progress;

	public int hours;

	public int minutes;

	public int seconds;

	private bool _timerComplete;

	private bool _timerIsReady;

	private TimeSpan _startTime;

	private TimeSpan _endTime;

	private TimeSpan _remainingTime;

	private float _value;

	public GameObject rewardUI;

	public GameObject rewardUIBG;

	public GameObject dailyRewardBtn;

	private Animator dailyRewardAnimator;

	public int minRewardCoinValue = 20;

	public int maxRewardCoinValue = 50;

	private void Awake()
	{
		LocalNotification.ClearNotifications();
	}

	private void Start()
	{
		dailyRewardAnimator = dailyRewardBtn.GetComponent<Animator>();
		if (PlayerPrefs.GetString("_timer") == "")
		{
			enableButton();
			return;
		}
		disableButton();
		StartCoroutine("CheckTime");
	}

	private void updateTime()
	{
		if (PlayerPrefs.GetString("_timer") == "Standby")
		{
			PlayerPrefs.SetString("_timer", TimeManager.sharedInstance.GetCurrentTimeNow());
			PlayerPrefs.SetString("_date", TimeManager.sharedInstance.GetCurrentDateNow());
		}
		else if (PlayerPrefs.GetString("_timer") != "" && PlayerPrefs.GetString("_timer") != "Standby")
		{
			DateTime dateTime = DateTime.ParseExact(PlayerPrefs.GetString("_date"), "yyyyMMdd", CultureInfo.CurrentCulture);
			DateTime dateTime2 = DateTime.ParseExact(TimeManager.sharedInstance.GetCurrentDateNow(), "yyyyMMdd", CultureInfo.CurrentCulture);
			if (dateTime2 > dateTime)
			{
				enableButton();
			}
			else if (dateTime2 == dateTime)
			{
				_configTimerSettings();
			}
			return;
		}
		_configTimerSettings();
	}

	private void _configTimerSettings()
	{
		_startTime = TimeSpan.Parse(PlayerPrefs.GetString("_timer"));
		_endTime = TimeSpan.Parse(hours + ":" + minutes + ":" + seconds);
		TimeSpan timeSpan = TimeSpan.Parse(TimeManager.sharedInstance.GetCurrentTimeNow()).Subtract(_startTime);
		_remainingTime = _endTime.Subtract(timeSpan);
		setProgressWhereWeLeftOff();
		if (timeSpan >= _endTime)
		{
			_timerComplete = true;
			enableButton();
		}
		else
		{
			_timerComplete = false;
			disableButton();
			_timerIsReady = true;
		}
	}

	private void setProgressWhereWeLeftOff()
	{
		float num = 1f / (float)_endTime.TotalSeconds;
		float num2 = 1f / (float)_remainingTime.TotalSeconds;
		_value = num / num2;
		_progress.fillAmount = _value;
	}

	private void enableButton()
	{
		timerButton.interactable = true;
		timeLabel.text = "CLAIM";
		dailyRewardAnimator.SetBool("activate", value: true);
		dailyRewardAnimator.SetBool("deactivate", value: false);
		LocalNotification.SendNotification(1, 1800000L, "Your gift is ready to be opened", "Tap to claim awesome gift", new Color32(byte.MaxValue, 68, 68, byte.MaxValue), true, true, true, "app_icon", null, "default");
	}

	private void disableButton()
	{
		timerButton.interactable = false;
		timeLabel.text = "WAIT";
		dailyRewardAnimator.SetBool("deactivate", value: true);
		dailyRewardAnimator.SetBool("activate", value: false);
	}

	private IEnumerator CheckTime()
	{
		disableButton();
		timeLabel.text = "---";
		yield return StartCoroutine(TimeManager.sharedInstance.GetTime());
		updateTime();
	}

	public void rewardClicked()
    {
        if (!AdManager1.instance.ShieldAddress) AdManager.single.ShowNative();
        if (timeLabel.text == "CLAIM")
		{
			PlayerPrefs.SetString("_timer", "Standby");
			StartCoroutine("CheckTime");
			dailyRewardAnimator.SetBool("deactivate", value: true);
			dailyRewardAnimator.SetBool("activate", value: false);
			float value = UnityEngine.Random.value;
			int reward = GetRandomRewardCoins() / 5 * 5;
			ShowRewardUI(reward);
			//FirebaseStat.Instance.LogSimpleEvent("ns_get_gift");
		}
	}

	private void Update()
	{
		if (_timerIsReady && !_timerComplete && PlayerPrefs.GetString("_timer") != "")
		{
			_value -= Time.deltaTime * 1f / (float)_endTime.TotalSeconds;
			_progress.fillAmount = _value;
			if (_value <= 0f && !_timerComplete)
			{
				validateTime();
				_timerComplete = true;
			}
		}
	}

	private void validateTime()
	{
		UnityEngine.Debug.Log("==> Validating time to make sure no speed hack!");
		StartCoroutine("CheckTime");
	}

	public void ShowRewardUI(int reward)
	{
		rewardUI.SetActive(value: true);
		rewardUIBG.SetActive(value: true);
		rewardUI.GetComponent<RewardUIController>().Reward(reward);
		RewardUIController.RewardUIClosed += OnRewardUIClosed;
	}

	private void OnRewardUIClosed()
	{
		RewardUIController.RewardUIClosed -= OnRewardUIClosed;
		dailyRewardBtn.SetActive(value: true);
	}

	public void HideRewardUI()
	{
		rewardUIBG.SetActive(value: false);
		rewardUI.GetComponent<RewardUIController>().Close();
	}

	private int GetRandomRewardCoins()
	{
		return UnityEngine.Random.Range(minRewardCoinValue, maxRewardCoinValue + 1);
	}
}
